Component({
  data: {
    visible: false,
    channel: "",
    payFee: null,
    hasDiscount: 0,
    enableAccount: true,
    curChannel: 'wx',
    balance: 0,
    bodyTop: 0,
    payChannels: [],
    account: {
      balance: 0,
      balance: 0,
      requirePassword: false,
      canUseDiscount: true,
    },
    showPwDialog: false,
    password: "",
    hasInit: false,
    isClick: false,
    isUseScoped:true,
  },
  observers: {
    'password': function (newValue) {
      if (newValue.length === 6) {
        this.handleInputPassword();
      }
    },
    'showPwDialog': function () {
      this.resetTop();
    },
  },
  methods: {
    checkPwKeyboard() {
      let showPwDialog = true;
      if (this.data.curChannel !== 'balance') {
        showPwDialog = false
      } else if (!this.data.account.requirePassword) {
        showPwDialog = false
      } else if (this.data.payFee > this.data.account.balance) {
        showPwDialog = false
      } else if (!this.data.account.canUseDiscount && this.data.hasDiscount) {
        showPwDialog = false
      }
      this.setData({
        showPwDialog: showPwDialog,
        hasInit: true
      })
    },
    resetTop() {
      let systemInfo = wx.getSystemInfoSync();
      let navHeight = systemInfo.statusBarHeight + 44;
      let clientHeight = systemInfo.screenHeight - navHeight;
      wx.createSelectorQuery().in(this).select('#pay-dialog-body').boundingClientRect((rect) => {
        console.info("rect", rect);
        let dialogHeight = rect.height;
        let bodyTop;
        if (!this.data.showPwDialog) {
          bodyTop = (clientHeight - dialogHeight) / 3 + navHeight;
        } else {
          console.info("440 / 750 * systemInfo.windowWidth", 440 / 750 * systemInfo.windowWidth)
          bodyTop = (clientHeight - dialogHeight - (440 / 750 * systemInfo.windowWidth)) / 3 + navHeight;
        }
        this.setData({
          bodyTop: bodyTop
        })
      }).exec()
    },
    changeChannel(e) {
      let channel = e.currentTarget.dataset.channel
      this.setData({
        curChannel: channel,
        password: ""
      })
      this.checkPwKeyboard();
    },
    handleShowPwKd() {
      this.setData({
        showPwDialog: true
      })
    },
    handleClose() {
      this.data.cancel && this.data.cancel();
      this.setData({
        visible: false
      })
    },
    handleStop() {
      return true;
    },
    handlePwInput(e) {
      this.setData({
        password: e.detail
      })
    },
    handleWxPay() {
      let self = this;
      if (this.data.isClick) {
        setTimeout(() => {
          self.setData({
            isClick: false
          })
        }, 500)
        return;
      } else {
        self.setData({
          isClick: true
        })
      }
      this.showLoading();
      this.data.confirm && this.data.confirm('wx');
    },
    handleBalancePay() {
      let self = this;
      if (this.data.isClick) {
        setTimeout(() => {
          self.setData({
            isClick: false
          })
        }, 500)
        return;
      } else {
        self.setData({
          isClick: true
        })
      }

      this.showLoading();
      this.data.confirm && this.data.confirm('balance', this.data.password);
    },
    handleInputPassword() {
      this.showLoading();
      this.data.confirm && this.data.confirm('balance', this.data.password);
    },
    /**
     * @param {Number} options.payFee  支付金额, 单位分
     * @param {Boolean} options.hasDiscount 是否有优惠
     * @param {Array<String>} options.payChannels ['account', 'wx'], account: 余额支付, wx:微信支付
     * @param {Number} options.account.balance 可用余额, 单位分
     * @param {Boolean} options.account.canUseDiscount 是否使用折扣
     * @param {Boolean} options.account.requirePassword 是否需要密码
     * @param {Function} options.confirm 回调函数
     * @param {Boolean} options.cancel 回调行数
     * */
    openPay(options) {
      let {
        payChannels,
        account,
        hasDiscount,
        payFee,
        confirm,
        cancel
      } = options;
      let curChannel = null;
      let enableAccount = false;
      if (payChannels.indexOf('balance') !== -1 && account.balance >= payFee && (!hasDiscount || account.canUseDiscount)) {
        curChannel = 'balance';
        enableAccount = true;
      }
      if (curChannel == null && payChannels.indexOf("wx") !== -1) {
        curChannel = 'wx';
      }
      if (curChannel === 'wx' && payChannels.length === 1) {
        this.setData({
          curChannel: curChannel,
          visible: false,
          confirm: confirm
        })
        this.handleWxPay();
        return;
      }
      if (curChannel == null) {
        curChannel = payChannels[0]
      }
      const openPayTime = wx.getStorageSync("openPayTime") || 1;
      console.log(new Date().getTime() - openPayTime > 2000)
      if (new Date().getTime() - openPayTime > 2000) {
        wx.setStorageSync("openPayTime", new Date().getTime())
        this.setData({
          payFee: payFee,
          hasDiscount: hasDiscount,
          payChannels: payChannels,
          showBalance: payChannels.indexOf('balance') !== -1,
          showWx: payChannels.indexOf('wx') !== -1 && payFee !== 0,
          curChannel: curChannel,
          account: account,
          enableAccount: enableAccount,
          visible: true,
          hasInit: false,
          password: "",
          confirm: confirm,
          cancel: cancel,
        })
      } else {
        wx.setStorageSync("openPayTime", new Date().getTime())
        return;
      }

      this.checkPwKeyboard();
    },
    showLoading() {
      this.selectComponent("#pay-toast").show({
        icon: this.data.curChannel
      });
    },

    hideLoading() {
      this.selectComponent("#pay-toast").hide()
    },
    resetPassword() {
      this.setData({
        password: ""
      })
    },
    close() {
      this.selectComponent("#pay-toast").hide()
      this.setData({
        visible: false
      })
    }
  }
});